package com.baidu.service.impl;

import com.baidu.mapper.TbSpecificationOptionMapper;
import com.baidu.pojo.TbSpecificationOption;
import com.baidu.utils.ResultResponse;
import com.baidu.vo.SpecificationVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.pojo.TbSpecification;
import com.baidu.service.TbSpecificationService;
import com.baidu.mapper.TbSpecificationMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class TbSpecificationServiceImpl extends ServiceImpl<TbSpecificationMapper, TbSpecification>
implements TbSpecificationService{
    @Autowired
    TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public ResultResponse findTbSpecificationList( Integer pageNum, Integer pageSize,TbSpecification tbSpecification) {
        Page<TbSpecification> page = new Page<>(pageNum, pageSize);
        Page<TbSpecification> list=tbSpecificationMapper.findTbSpecificationList( page,tbSpecification.getSpecName());
        return ResultResponse.SUCCESS(list);
    }

    @Override
    @Transactional
    public void saveTbSpecification(SpecificationVo specificationVo) {
        TbSpecification tbSpecification = specificationVo.getSpec();
        tbSpecificationMapper.insert(tbSpecification);
        List<TbSpecificationOption> options = specificationVo.getOptionList();

        TbSpecification sp = tbSpecificationMapper.selectById(tbSpecification.getId());

        for (TbSpecificationOption option : options) {
            option.setSpecId(sp.getId());
            tbSpecificationOptionMapper.insert(option);
        }
    }

    @Override
    public TbSpecification queryTbSpecificationById(Long id) {
        return tbSpecificationMapper.selectById(id);
    }

    @Override
    public void updateTbSpecification(SpecificationVo specificationVo) {
        TbSpecification spec = specificationVo.getSpec();
        TbSpecification tbSpecification = tbSpecificationMapper.selectById(spec.getId());
        Long id = spec.getId();
        tbSpecificationOptionMapper.deleteBySpecId(id);

        List<TbSpecificationOption> optionList = specificationVo.getOptionList();
        for (TbSpecificationOption option : optionList) {
            option.setSpecId(tbSpecification.getId());
            tbSpecificationOptionMapper.insert(option);
        }

    }

    @Override
    public ResultResponse del(List<TbSpecification> ids) {
        for (TbSpecification tbspecification : ids) {
            tbSpecificationMapper.deleteById(tbspecification.getId());
            tbSpecificationOptionMapper.deleteBySpecId(tbspecification.getId());
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public List<TbSpecification> findList() {
        return tbSpecificationMapper.selectList(null);
    }
}




